
GLOBAL.setmetatable(env, {
    __index = function(t, k)
        return GLOBAL.rawget(GLOBAL, k)
    end
})

Assets = { Asset("ANIM", "anim/obrsword_power.zip"), Asset("ANIM", "anim/obro.zip"),
    Asset("SOUNDPACKAGE", "sound/power_change.fev"),       -- 命名有误，这里是打击音效
    Asset("SOUND", "sound/power_change.fsb"), Asset("SOUNDPACKAGE", "sound/obrost.fev"), Asset("SOUND",
    "sound/obrost.fsb") }

SpawnPrefab = GLOBAL.SpawnPrefab
local containers = require "containers"
local params = containers.params
local skp = require 'util/obr_opskill'
-- 特效浏览器
-- 23SAND 64MOLE 86-89WATER  95LIGHT  100KINFE  101-104 347 FIRE 166DIRT  210WATER 325LIGHT

PrefabFiles = { "obrsword", "sword_fire_projectile", "power_knife", "firepit_firebird", "obsword_wind_torado",
    "sword_fx_water", "sword_fx_water2", "sword_fx_water3", "obrsword_powerchange", "obrsword_laser",
    "obrrock", "obsword_shield", "swap_obrsword_fx", "exswapobs", "exswapobs2" }

AddReplicableComponent("obrsword_power")
-- 数据配置
TUNING.WILSON_SANITYVALUE_HOP_ATLEAST = 30
TUNING.swordtype = 0
TUNING.FIRECONSUE = 90 --#region  所有技能的统一能量消耗
TUNING.dirt_damage = 200
TUNING.fire_damage = 175
TUNING.water_damage = 100
TUNING.wind_damage = 150
--
TUNING.ANTLION = false
TUNING.MOOSE = false
TUNING.DRAGONFLY = false

--配置引入
TUNING.Obrsword_navoice = GetModConfigData('Obrsword_navoice') or true
TUNING.Obrsword_nab = GetModConfigData('Obrsword_nab') or true
TUNING.Obrsword_nadamage = GetModConfigData('Obrsword_nadamage') or 30
-- modimport('scripts/util/obrsword_laser_actions.lua')
modimport("scripts/util/obrsword_power_ui")
modimport('scripts/util/filterimg')
modimport('scripts/util/DPS')
modimport('scripts/util/mapgoto')
-----------------------

STRINGS.NAMES.OBRSWORD = "欧布圣剑" -- 显示的名字
STRINGS.CHARACTERS.GENERIC.DESCRIBE.OBRSWORD = "神秘的宇宙来客" -- 人物检查时说的话
-- STRINGS.RECIPE_DESC.OBRSWORD = "进一步利用臭鸟蛋。" -- 制作栏描述
-----------格子
params.obrsword = {
    widget = {
        slotpos = { Vector3(0, 32 + 4, 0) },
        slotbg = { {
            image = "cook_slot_spice.tex"
        } },
        animbank = "ui_cookpot_1x2",
        animbuild = "ui_cookpot_1x2",
        pos = Vector3(0, 15, 0)
    },
    acceptsstacks = false,
    usespecificslotsforitems = true,
    type = "hand_inv",
    excludefromcrafting = true
}

function params.obrsword.itemtestfn(container, item, slot)
    return item.prefab == "redgem" or item.prefab == "opalpreciousgem" or item.prefab == "bluegem" or item.prefab ==
        "purplegem" or item.prefab == "greengem" or item.prefab == "yellowgem"
        or item.prefab == "orangegem" or item.prefab == "panflute"
end

--[[
function SimInit(inst)
	print("spawning item")
	local prefab = SpawnPrefab("obrsword")
	inst.components.inventory:GiveItem(prefab)
end

AddSimPostInit(SimInit)
]] --

--- --
---@param player any
local function GrowGiant(player)
    if player.components.inventory.equipslots.hands ~= nil then
        local weapon = player.components.inventory.equipslots.hands
        if not weapon:HasTag("obrsword") then
            return
        end

        if TUNING.swordtype == 6 then
            TUNING.swordtype = 0
        else
            TUNING.swordtype = TUNING.swordtype + 1
            if TUNING.swordtype < 3 then
                weapon:PushEvent("changepower", {})
                return
            end

            local op = player.components.obrsword_power
            print("dragonfly:", op.dragonfly)
            print("moose:", op.moose)
            print("antlion:", op.antlion)


            if TUNING.swordtype == 3 and op.dragonfly == false then
                TUNING.swordtype = TUNING.swordtype + 1
            end
            if TUNING.swordtype == 4 and op.moose == false then
                TUNING.swordtype = TUNING.swordtype + 1
            end
            if TUNING.swordtype == 5 and op.antlion == false then
                TUNING.swordtype = 0
            end
        end

        weapon:PushEvent("changepower", {})
    end
end
-- This adds the handler, which means that if the server gets told "GrowGiant",
-- it will call our function, GrowGiant, above
AddModRPCHandler(modname, "GrowGiant", GrowGiant)
-- This has it send the RPC to the server when you press "v"
local function SendGrowGiantRPC()
    SendModRPCToServer(MOD_RPC[modname]["GrowGiant"])
end
-- This just uses keycodes, which you can look up online. This one is "v".
GLOBAL.TheInput:AddKeyDownHandler(KEY_J, SendGrowGiantRPC)

local function Showfilter(player) ----给客机发送的rpc
    player.HUD.filter_uianim:Show()
    player.HUD.inst:DoTaskInTime(8, function()
        player.HUD.filter_uianim:Hide()
    end)
end

AddPlayerPostInit(function(inst)
    inst.niniang_fn = net_event(inst.GUID, "obrbeam.netevent")

    if not TheWorld.ismastersim or not TheNet:IsDedicated() then
        inst:ListenForEvent("obrbeam.netevent", Showfilter)
    end
    if not TheWorld.ismastersim then return end
  
   
    local ex =inst.components.obrsword_power.extrapower + 100

   inst.components.obrsword_power:SetMax(ex)
   inst.components.obrsword_power:DoDelta(0)
end)

AddClientModRPCHandler(modname, "Showfilter", Showfilter)
function SendSendModRPCToClientRPC(player)
    SendModRPCToClient(MOD_RPC[modname]["Showfilter"], player.userid, Showfilter)
end

-----------------
